package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.cache.CacheOperationType;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.cache.GuiGuCached;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 根据alumbId获取专辑详情 调用不同的微服务使用异步编排优化性能
     * albumInfo announcer albumStatVo baseCategoryView
     *
     * @param albumId
     * @return
     */
    @Override
    //@GuiGuCache(prefix = "search:albuminfo:")
    @GuiGuCached(prefix = "album:searchinfo:")
    public Map<String, Object> albumInfo(Long albumId) {
        Map<String, Object> map = new ConcurrentHashMap<>();
        log.info("albumId："+albumId, albumId);
        if(albumId == null){
            return new HashMap<>();
        }
        // 1.获取专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            log.info("专辑信息");
            Assert.notNull(albumInfo,"专辑：{}不存在",albumId);
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor);
        // 2.获取主播信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Long userId = albumInfo.getUserId();
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(userId).getData();
            log.info("主播信息");
            Assert.notNull(userInfoVo, "用户：{}不存在", userId);
            map.put("announcer", userInfoVo);
        }, threadPoolExecutor);
        // 3.获取专辑分类信息(category3Id)
        CompletableFuture<Void> categoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Long category3Id = albumInfo.getCategory3Id();
            BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(category3Id).getData();
            log.info("专辑分类信息");
            Assert.notNull(baseCategoryView, "分类：{}不存在", category3Id);
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);
        // 4.获取专辑属性信息
        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            log.info("专辑属性信息");
            Assert.notNull(albumStatVo, "专辑：{}不存在", albumId);
            map.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor);

        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                announcerCompletableFuture,
                categoryViewCompletableFuture,
                albumStatVoCompletableFuture
        ).join();

        return map;
    }
}
